package com.letv.core.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.letv.core.bean.LiveBeanLeChannel;
import com.letv.core.bean.LiveBeanLeChannelList;
import com.letv.core.constant.DatabaseConstant;
import com.letv.core.constant.LetvConstant;
import com.letv.core.utils.LetvTools;
import com.letv.core.utils.LetvUtils;
import com.letv.core.utils.LogInfo;

/**
 * 适用： 本地缓存和历史信息
 * @author libo5
 *
 */
public class ChannelHisListHandler {
	private Context mContext;
	public ChannelHisListHandler(Context mContext){
		this.mContext=mContext;
	}
	/**
	 * 更新频道表 
	 */
	public synchronized boolean updateByChannelId(LiveBeanLeChannel liveLunboWeishi, String type){
		try {
			if(liveLunboWeishi!=null){
				ContentValues values=new ContentValues();
				values.put(DatabaseConstant.ChannelHisListTrace.Field.CHANNELID, liveLunboWeishi.getChannelId());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.NUMERICKEYS, liveLunboWeishi.getNumericKeys());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.NAME, liveLunboWeishi.getChannelName());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.ENAME, liveLunboWeishi.getChannelEname());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.ISRECORD, liveLunboWeishi.getIsRecord());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.SYSTEMILLISECOND, liveLunboWeishi.getCurrentmillisecond());
				mContext.getContentResolver().update(LetvContentProvider.URI_CHANNELHISLISTTRACE, values,
						DatabaseConstant.ChannelHisListTrace.Field.CHANNELID + "=? and " + DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?", new String[] {liveLunboWeishi.getChannelId()+"", type});
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}	
	}
	/**
	 * 更新频道表  使用排序号更新
	 */
	public synchronized boolean updateByNumberkeys(LiveBeanLeChannel liveLunboWeishi,String channelStatus, String type){
		try {
			if(liveLunboWeishi!=null){
				ContentValues values=new ContentValues();
				values.put(DatabaseConstant.ChannelHisListTrace.Field.CHANNELID, liveLunboWeishi.getChannelId());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.NUMERICKEYS, liveLunboWeishi.getNumericKeys());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.NAME, liveLunboWeishi.getChannelName());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.ENAME, liveLunboWeishi.getChannelEname());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.ISRECORD, liveLunboWeishi.getIsRecord());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.SYSTEMILLISECOND, liveLunboWeishi.getCurrentmillisecond());
				values.put(DatabaseConstant.ChannelHisListTrace.Field.CHANNELSTATUS, channelStatus);
				mContext.getContentResolver().update(LetvContentProvider.URI_CHANNELHISLISTTRACE, values,
						DatabaseConstant.ChannelHisListTrace.Field.NUMERICKEYS + "=? and " + DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?", new String[] {liveLunboWeishi.getNumericKeys()+"", type});
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}	
	}
/**
 * 更新历史（缓存）频道状态
 * @param channelId
 * @param channelStatus
 * @return
 */
	public synchronized boolean updateChannelStatus(LiveBeanLeChannel channel,String channelStatus, String type){
		ContentValues values = new ContentValues();
		values.put(DatabaseConstant.ChannelHisListTrace.Field.CHANNELID, channel.getChannelId());
		values.put(DatabaseConstant.ChannelHisListTrace.Field.NUMERICKEYS, channel.getNumericKeys());
		values.put(DatabaseConstant.ChannelHisListTrace.Field.NAME, channel.getChannelName());
		values.put(DatabaseConstant.ChannelHisListTrace.Field.ENAME, channel.getChannelEname());
		values.put(DatabaseConstant.ChannelHisListTrace.Field.CHANNELSTATUS, channelStatus);
		values.put(DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE, type);
        try {
            return mContext.getContentResolver().update(
                    LetvContentProvider.URI_CHANNELHISLISTTRACE,
                    values,
                    DatabaseConstant.ChannelHisListTrace.Field.CHANNELID + "=? and " + DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?",
                    new String[]{channel.getChannelId(), type}) == 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
	}
	/**
	 * 更新历史（缓存）频道状态
	 * @param channelId
	 * @param channelStatus
	 * @return
	 */
	public synchronized boolean delete(LiveBeanLeChannel channel,String type){
        try {
            return mContext.getContentResolver().delete(LetvContentProvider.URI_CHANNELHISLISTTRACE, DatabaseConstant.ChannelHisListTrace.Field.CHANNELID + "=? and " + DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?",
                    new String[]{channel.getChannelId(), type})>0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
	}
    /**
     * 更新频道历史
     * @param channelId
     * @return
     */
    public boolean updateHisChannel(String channelId, String type) {
        try {
            ContentValues cv = new ContentValues();
            cv.put(DatabaseConstant.ChannelHisListTrace.Field.ISRECORD, 1);
            cv.put(DatabaseConstant.ChannelHisListTrace.Field.SYSTEMILLISECOND, System.currentTimeMillis());
            
            mContext.getContentResolver().update(LetvContentProvider.URI_CHANNELHISLISTTRACE, cv,
                    DatabaseConstant.ChannelHisListTrace.Field.CHANNELID + "=? and " + DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?",
                    new String[] { channelId, type });
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
	
	/**
	 * 获取最新的频道历史
	 * @return
	 */
	public LiveBeanLeChannel getLastHisChannel(String type){
		Cursor cursor = null;
		try {
			cursor = mContext.getContentResolver().query(LetvContentProvider.URI_CHANNELHISLISTTRACE, null, 
					/*DatabaseConstant.ChannelHisListTrace.Field.CHANNELSTATUS + "= ? and " + */DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?",
					new String[] { /*DatabaseConstant.ChannelHisListTrace.ChannelStatus.NORMAL + "",*/ type },
					DatabaseConstant.ChannelHisListTrace.Field.SYSTEMILLISECOND+" desc");
			if(cursor != null && cursor.moveToFirst()){
				LiveBeanLeChannel livelunboweishi=new LiveBeanLeChannel();
				livelunboweishi.setChannelId(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.CHANNELID)));
				livelunboweishi.setNumericKeys(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.NUMERICKEYS)));
				livelunboweishi.setChannelName(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.NAME)));
				livelunboweishi.setChannelEname(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.ENAME)));
				livelunboweishi.setCurrentmillisecond(cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.SYSTEMILLISECOND)));
				return livelunboweishi;
			}
		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			LetvTools.closeCursor(cursor);
		}
		return null;
	}
	
	/**
	 * 获取历史的Channel
	 */
	public LiveBeanLeChannelList getHisChannelList(String type){
		Cursor cursor = null;
		LiveBeanLeChannelList lunboWeishiList = null;
		try {
			cursor = mContext.getContentResolver().query(
					LetvContentProvider.URI_CHANNELHISLISTTRACE, null,
					DatabaseConstant.ChannelHisListTrace.Field.ISRECORD + "= ? " +
							//"and "+DatabaseConstant.ChannelHisListTrace.Field.CHANNELSTATUS+"= ?" +
									" and " + DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?",
					new String[] { 1 + "",/*DatabaseConstant.ChannelHisListTrace.ChannelStatus.NORMAL + "",*/ type}, null);
			lunboWeishiList=new LiveBeanLeChannelList(); 
			while (cursor.moveToNext()) {
				LiveBeanLeChannel livelunboweishi=new LiveBeanLeChannel();
				livelunboweishi.setChannelId(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.CHANNELID)));
				if ("weishi".equals(type)) {
					livelunboweishi.setNumericKeys("");
				}else{
					livelunboweishi.setNumericKeys(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.NUMERICKEYS)));
				}
				livelunboweishi.setChannelName(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.NAME)));
				livelunboweishi.setChannelEname(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.ENAME)));
//				livelunboweishi.setBeginTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.BEGINTIME)));
//				livelunboweishi.setEndTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.ENDTIME)));
//				livelunboweishi.setChannelClass(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.CHANNELCLASS)));
//				livelunboweishi.setBelongBrand(cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.BELONGBRAND)));
//				livelunboweishi.setSourceId(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.SOURCEID)));
//				livelunboweishi.setIs3D(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.IS3D)));
//				livelunboweishi.setIs4K(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.IS4K)));
//				livelunboweishi.setCh(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.CH)));
//				livelunboweishi.setOrderNo(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.ORDERNO)));
//				livelunboweishi.setIsRecommend(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.ISCOMMEND)));
//				livelunboweishi.setLogo120x90(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO120X90)));
//				livelunboweishi.setLogoOrigin(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGOORIGIN)));
//				livelunboweishi.setLogo150x200(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO150X200)));
//				livelunboweishi.setLogo120x160(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO120X160)));
//				livelunboweishi.setLogo96x128(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO96X128)));
//				livelunboweishi.setLogo150x150(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO150X150)));
//				livelunboweishi.setLogo30x30(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO30X30)));
				livelunboweishi.setCurrentmillisecond(cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.SYSTEMILLISECOND)));
				lunboWeishiList.add(livelunboweishi);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			LetvTools.closeCursor(cursor);
		}
		return lunboWeishiList;
	}
	/**
		历史列表中是否存在数据
	**/
	public boolean hasHisTableData(String type){
		Cursor cursor = null;
		try {
			cursor = mContext.getContentResolver().query(
					LetvContentProvider.URI_CHANNELHISLISTTRACE, null,
					DatabaseConstant.ChannelHisListTrace.Field.ISRECORD + "= ? and " + DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?",
					new String[] { 1 + "", type }, null);
			return cursor.getCount() > 0;
		}catch(Exception e) {

			e.printStackTrace();
			return false;
		}finally {
			LetvTools.closeCursor(cursor);
		}
	}
	/**
	 * 得到所有的频道
	 */
	public LiveBeanLeChannelList getAllChannelList(String type){
		Cursor cursor = null;
		LiveBeanLeChannelList lunboWeishiList = null;
		try {
			cursor=mContext.getContentResolver().query(LetvContentProvider.URI_CHANNELHISLISTTRACE, null,
					DatabaseConstant.ChannelHisListTrace.Field.CHANNELSTATUS + "= ? and " + DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?",
					new String[] { DatabaseConstant.ChannelHisListTrace.ChannelStatus.NORMAL + "", type }, null);
			lunboWeishiList=new LiveBeanLeChannelList(); 
			while (cursor.moveToNext()) {
				LiveBeanLeChannel livelunboweishi=new LiveBeanLeChannel();
				livelunboweishi.setChannelId(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.CHANNELID)));
				livelunboweishi.setNumericKeys(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.NUMERICKEYS)));
				livelunboweishi.setChannelName(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.NAME)));
				livelunboweishi.setChannelEname(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.ENAME)));
//				livelunboweishi.setBeginTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.BEGINTIME)));
//				livelunboweishi.setEndTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.ENDTIME)));
//				livelunboweishi.setChannelClass(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.CHANNELCLASS)));
//				livelunboweishi.setBelongBrand(cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.BELONGBRAND)));
//				livelunboweishi.setSourceId(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.SOURCEID)));
//				livelunboweishi.setIs3D(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.IS3D)));
//				livelunboweishi.setIs4K(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.IS4K)));
//				livelunboweishi.setCh(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.CH)));
//				livelunboweishi.setOrderNo(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.ORDERNO)));
//				livelunboweishi.setIsRecommend(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.ISCOMMEND)));
//				livelunboweishi.setLogo120x90(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO120X90)));
//				livelunboweishi.setLogoOrigin(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGOORIGIN)));
//				livelunboweishi.setLogo150x200(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO150X200)));
//				livelunboweishi.setLogo120x160(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO120X160)));
//				livelunboweishi.setLogo96x128(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO96X128)));
//				livelunboweishi.setLogo150x150(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO150X150)));
//				livelunboweishi.setLogo30x30(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO30X30)));
				livelunboweishi.setCurrentmillisecond(cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.SYSTEMILLISECOND)));
				lunboWeishiList.add(livelunboweishi);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			LetvTools.closeCursor(cursor);
		}
		return lunboWeishiList;
	}

	public synchronized boolean addToChannelList(LiveBeanLeChannel liveLunboWeishi, String type){
		try {
			if(liveLunboWeishi!=null){
				if(isExist(liveLunboWeishi.getChannelId(), type)){
					return true;
				}
			}
			ContentValues values=new ContentValues();
			values.put(DatabaseConstant.ChannelHisListTrace.Field.CHANNELID, liveLunboWeishi.getChannelId());
			values.put(DatabaseConstant.ChannelHisListTrace.Field.NUMERICKEYS, liveLunboWeishi.getNumericKeys());
			values.put(DatabaseConstant.ChannelHisListTrace.Field.NAME, liveLunboWeishi.getChannelName());
			values.put(DatabaseConstant.ChannelHisListTrace.Field.ENAME, liveLunboWeishi.getChannelEname());
			values.put(DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE, type);
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.BEGINTIME, liveLunboWeishi.getBeginTime());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.ENDTIME, liveLunboWeishi.getEndTime());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.CHANNELCLASS, liveLunboWeishi.getChannelClass());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.BELONGBRAND, liveLunboWeishi.getBelongBrand());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.SOURCEID, liveLunboWeishi.getSourceId());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.IS3D, liveLunboWeishi.getIs3D());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.IS4K, liveLunboWeishi.getIs4K());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.CH,liveLunboWeishi.getCh());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.ORDERNO,liveLunboWeishi.getOrderNo());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.ISCOMMEND,liveLunboWeishi.getIsRecommend());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.LOGO120X90,liveLunboWeishi.getLogo120x90());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.LOGOORIGIN,liveLunboWeishi.getLogoOrigin());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.LOGO150X200,liveLunboWeishi.getLogo150x200());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.LOGO120X160,liveLunboWeishi.getLogo120x160());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.LOGO96X128,liveLunboWeishi.getLogo96x128());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.LOGO150X150,liveLunboWeishi.getLogo150x150());
//			values.put(DatabaseConstant.ChannelHisListTrace.Field.LOGO30X30,liveLunboWeishi.getLogo30x30());
			mContext.getContentResolver().insert(LetvContentProvider.URI_CHANNELHISLISTTRACE, values);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 是否存在
	 * @param ename
	 * @return
	 */
	public boolean isExist(String channelId, String type){
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_CHANNELHISLISTTRACE, null,
					DatabaseConstant.ChannelHisListTrace.Field.CHANNELID + "=? and " + DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?",
					new String[]{channelId + "", type}, null);
            if (cursor != null && cursor.getCount() > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            LetvTools.closeCursor(cursor);
        }
        return false;
    }
	/**
	 * 根据排序号查找和下线状态查找
	 * @param numberKeys
	 * @return
	 */
	public boolean isExistByNumberKeysAndStatus(String numberKeys, String type){
		 Cursor cursor = null;
	        try {
	            cursor = mContext.getContentResolver().
	            		query(LetvContentProvider.URI_CHANNELHISLISTTRACE, 
	            											null, 
	            											DatabaseConstant.ChannelHisListTrace.Field.NUMERICKEYS + "=? and "+DatabaseConstant.ChannelHisListTrace.Field.CHANNELSTATUS+"= ? and " + DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?",
	            											new String[]{numberKeys + "",DatabaseConstant.ChannelHisListTrace.ChannelStatus.DELETE + "", type},
	            											null);
	            if (cursor != null && cursor.getCount() > 0) {
	                return true;
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            LetvTools.closeCursor(cursor);
	        }
	        return false;
	}
    /**
     * 获取数据库第一条数据的位置
     * @return
     */
    public int getFirstChannelPosition(String type) {
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_CHANNELHISLISTTRACE, null, 
            		DatabaseConstant.ChannelHisListTrace.Field.CHANNELSTATUS + "= ? and " + DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?",
					new String[] { DatabaseConstant.ChannelHisListTrace.ChannelStatus.NORMAL + "", type },
            		null);
            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToNext();
                return cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            LetvTools.closeCursor(cursor);
        }

        return 0;
    }

    public int getCurrentChannelPosition(String curChannelName, String type) {
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(
                    LetvContentProvider.URI_CHANNELHISLISTTRACE, null,
                    DatabaseConstant.ChannelHisListTrace.Field.NAME + "= ? and " + DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?",
                    new String[]{curChannelName, type}, null);

            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToNext();
                return cursor.getInt(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            LetvTools.closeCursor(cursor);
        }

        return -1;
    }

    /**
     * 通过当前正在播放的频道名称获取下一个频道
     * @param curChannelName
     */
    public LiveBeanLeChannel getNextChannel(String curChannelName, String type) {
        Cursor cursor = null;
        try {
            int position = getCurrentChannelPosition(curChannelName, type);
            LogInfo.log("ljnalex", "getNextChannel position = " + position);
            if (position != -1) {
            	do {
            		  cursor = mContext.getContentResolver().query(
                              LetvContentProvider.URI_CHANNELHISLISTTRACE, null,
                              "_id = ? and " + DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?",
							  new String[]{position + 1 + "", type}, null);
            		  cursor.moveToNext();
				} while (cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.CHANNELSTATUS)).equals(DatabaseConstant.ChannelHisListTrace.ChannelStatus.DELETE));
                if (cursor != null && cursor.getCount() > 0) {
                   
                    return createChannel(cursor);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            LetvTools.closeCursor(cursor);
        }

        return null;
    }

    /**
     * 通过当前正在播放的频道名称获取上一个频道
     * @param curChannelName
     */
    public LiveBeanLeChannel getPreChannel(String curChannelName, String type) {
        Cursor cursor = null;
        try {
            int position = getCurrentChannelPosition(curChannelName, type);
            LogInfo.log("ljnalex", "getPreChannel position = " + position);
            if (position != -1) {
            	do {
                    cursor = mContext.getContentResolver().query(
                            LetvContentProvider.URI_CHANNELHISLISTTRACE, null,
                            "_id = ? and " + DatabaseConstant.ChannelHisListTrace.Field.CHANNEL_TYPE + "=?",
							new String[]{(position - 1) + "", type}, null);
                    cursor.moveToLast();
				} while (cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.CHANNELSTATUS)).equals(DatabaseConstant.ChannelHisListTrace.ChannelStatus.DELETE));
                if (cursor != null && cursor.getCount() > 0) {
                    return createChannel(cursor);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            LetvTools.closeCursor(cursor);
        }

        return null;
    }

    private LiveBeanLeChannel createChannel(Cursor cursor) {


		LiveBeanLeChannel livelunboweishi=new LiveBeanLeChannel();
		try {
			livelunboweishi.setChannelId(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.CHANNELID)));
			livelunboweishi.setNumericKeys(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.NUMERICKEYS)));
			livelunboweishi.setChannelName(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.NAME)));
			livelunboweishi.setChannelEname(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.ENAME)));
		}catch(Exception e) {

			e.printStackTrace();
		}finally {
			LetvTools.closeCursor(cursor);
		}
//        livelunboweishi.setBeginTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.BEGINTIME)));
//        livelunboweishi.setEndTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.ENDTIME)));
//        livelunboweishi.setChannelClass(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.CHANNELCLASS)));
//        livelunboweishi.setBelongBrand(cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.BELONGBRAND)));
//        livelunboweishi.setSourceId(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.SOURCEID)));
//        livelunboweishi.setIs3D(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.IS3D)));
//        livelunboweishi.setIs4K(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.IS4K)));
//        livelunboweishi.setCh(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.CH)));
//        livelunboweishi.setOrderNo(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.ORDERNO)));
//        livelunboweishi.setIsRecommend(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.ISCOMMEND)));
//        livelunboweishi.setLogo120x90(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO120X90)));
//        livelunboweishi.setLogoOrigin(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGOORIGIN)));
//        livelunboweishi.setLogo150x200(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO150X200)));
//        livelunboweishi.setLogo120x160(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO120X160)));
//        livelunboweishi.setLogo96x128(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO96X128)));
//        livelunboweishi.setLogo150x150(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO150X150)));
//        livelunboweishi.setLogo30x30(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseConstant.ChannelHisListTrace.Field.LOGO30X30)));
        return livelunboweishi;
    }
}
